#lang racket

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

(define (id x) x)

(define (cube x) (* x x x))

(define (inc x) (+ x 1))

(sum id 1 inc 10)


(define (simpson-integral f a b n)
  (define h (/ (- b a) n))
  (define (koef k)
    (cond ((or (= k 0) (= k n)) 1)
          ((odd? k) 4)
          (else 2)))
  (define (iter k)
    (if (= n k)
        0
        (+ (* (koef k) (f (+ a (* k h)))) 
           (iter(+ k 1)))))
  (/ (* h (iter 0)) 3))

(simpson-integral cube 0 1 1000)